<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/7 0007
 * Time: 17:08
 */

namespace app\index\controller;


use app\common\RedisCache;
use app\common\wx_common;
use think\Controller;
use think\Db;

class Base extends Controller
{
    public $mobile;
    public $ip;
    public $params;
    public $token;
    public $device_id = '';
    //1:未登陆，2:登陆失效 3:登陆成功
    public $loginStatus = 1;
    public $site_id;
    public function __construct()
    {
        parent::__construct();
        $this->ip = $this->getIp();
        $this->params = input();
        $this->token = input('token');
        $this->mobile = session('mobile');
        $this->device_id = session('device_id');
        if($this->token){
            $token = session('token');
            if(!$token){
                session('token',$this->token);
            }else if($token != $this->token){
                //用户换账户，重新登录
                session('token',$this->token);
                $this->login();
            }
        } else {
            /// 当token为空访问页面为活动首页时，清空会话。
            $index_url = url('index');
            if (substr(request()->baseUrl(), 0, strlen($index_url)) === $index_url) {
                session(null);
            }
        }
        $site = input('site');
        if ($site) {
            session('site_id', $site);
        }
        $this->site_id = $site ?: session('site_id');
        if(!$this->mobile){
            $this->login();
        }else{
            $this->loginStatus = 3;
        }
//        $isLogin = $this->mobile?1:0;
        $this->assign('is_login',$this->mobile?1:0);
        $this->assign('app', $this->getSiteConfig());
    }

    public function getSiteConfig()
    {
        $site = Db::name('site')
            ->alias('a')
            ->join('site_config b','a.id=b.site_id','left')
            ->where('a.id',$this->site_id)
            ->find();
        //是否允许web端调试
        $site['enable_web'] = env('ENABLE_WEB',0);
        $this->assign('site_config',$site);
        return $site;
    }

    /**
     * @return bool|int
     * false 表明登陆超过
     * 1 表明未登陆
     * 2 表明登陆已失效
     */
    public function login()
    {
        $token = session('token');
        if(!$token){
            $this->loginStatus = 1;
            return false;
        }
        if(env('API_TOKEN_DEMO',false)){
            $this->mobile ='13012341234';
            if($token==1){
                $this->mobile = '13012341111';
            }
            if($token==2){
                $this->mobile = '13012342222';
            }
            if($token==3){
                $this->mobile = '13012343333';
            }
            if($token==4){
                $this->mobile = '13012344444';
            }
            if($token==5){
                $this->mobile = '13012345555';
            }
        }elseif($token == 99){
            $this->mobile = '13999999999';
        }else{
            $config = $this->getSiteConfig();
            //调用后端接口token获取手机号
            $url = $config['app_login_api'];
            $res = creat_curl($url,['token'=>$token]);
            $res = json_decode($res,1);
            if($res['rs'] == 200 && !empty($res['data']['phoneNumber'])){
                $this->mobile = $res['data']['phoneNumber'];
            }else{
                $this->loginStatus = 2;
                return false;
            }
        }
        $this->device_id = input('deviceId');
        session('device_id',$this->device_id);
        session('mobile',$this->mobile);
        $this->loginStatus = 3;
        return true;
    }

    public function isLogin()
    {
        if(!$this->mobile){
            if($this->loginStatus == 1)
                return jsonReturn(500,'请先登陆');
            return jsonReturn(501,'登录失效，请重新登录');
        }
        return false;
    }

    public function getIp()
    {
        $ip = request()->ip();
        return $ip;
    }

    public function getPage()
    {
        return input('page/d')?:1;
    }

}